<?php
function bend_order(&$retparams){
	global $_db;
	
	if(!isset($_POST['op'])){
		return BEND_REDIRECT;
	}
	elseif("addview"==$_POST['op']){
		echo rendertpl("applet/orderaddmod.applet");
		return BEND_RENDERED;
	}
	elseif("add"==$_POST['op']){
		//如果要新增客户
		if("-1"==@$_POST['customerid']){
			include_once ROOT_DIR.'/bc/block.percust.php';
			$percustparams = array();
			foreach($_POST as $k => $v){
				if(substr($k, 0, 3)=="pc_")
					$percustparams[substr($k,3)] = $v;
			}
			$cid = addpercust($percustparams);
			if($cid>=1){
				$_POST['customerid'] = $cid;
			}
			else{
				echo "新增客户失败";
				return BEND_RENDERED;
			}
		}
		//生成订单编号
		$userid = $_SESSION['userid'];
		if($userid<0) $userid=0;
		$userid .= "";
		$serialnum = substr($userid, -1)."-";//用户id末位
		$serialnum .= date("ymd");//年月日
		$curmax = $_db->get1stcolval("select count(*) from t_order where c_created>=".parsedate(date("Y-m-d"), 'Y-m-d'));
		$curmax ++;
		$serialnum .= ($curmax%1000);//(当天已新增的订单个数+1)%1000
		$serialnum .= rand(0, 9);//0-9中的一个随机数
		
		$id = $_db->insert("order"
			,array("serialnum"=>$serialnum, "customerid"=>@$_POST['customerid'], "status"=>@$_POST['status']
				, "prodid"=>@$_POST['prodid'], "plannum"=>@$_POST['plannum'], "planperiod"=>@$_POST['planperiod']
				, "applynum"=>@$_POST['applynum'], "contractnum"=>@$_POST['contractnum'], "actualnum"=>@$_POST['actualnum']
				, "period"=>@$_POST['period'], "touserid"=>@$_POST['touserid'], "tofee"=>@$_POST['tofee'], "didtopay"=>@$_POST['didtopay']
				, "remark"=>@$_POST['remark'], "srctype"=>@$_POST['srctype'], "srcuserid"=>@$_POST['srcuserid']
				, "srcfee"=>@$_POST['srcfee'], "didpaysrc"=>@$_POST['didpaysrc'], "recuserid"=>@$_POST['recuserid']
				, "recfee"=>@$_POST['recfee'], "didpayrec"=>@$_POST['didpayrec'], "lastcomm"=>parsedate(@$_POST['lastcomm'])
				, "nextcomm"=>parsedate(@$_POST['nextcomm']), "commrecord"=>@$_POST['commrecord'], "evalcustomer"=>@$_POST['evalcustomer']
				, "toremark"=>@$_POST['toremark'], "srcremark"=>@$_POST['srcremark'], "recremark"=>@$_POST['recremark'])
			);
		if($id>0){
			$retparams['errorcode'] = 0;
		}
		else{
			$retparams['errorcode'] = -1;
		}
		return BEND_ECHORET;
	}
	elseif("del"==$_POST['op']){
		$count = $_db->delete("order", "id=$_POST[id]");
	}
	elseif("modview"==$_POST['op']){
		echo rendertpl("applet/orderaddmod.applet");
		return BEND_RENDERED;
	}
	elseif("mod"==$_POST['op']){
		$count = $_db->update("order"
			,array("customerid"=>@$_POST['customerid'], "status"=>@$_POST['status'], "prodid"=>@$_POST['prodid']
				,"plannum"=>@$_POST['plannum'], "planperiod"=>@$_POST['planperiod'], "applynum"=>@$_POST['applynum']
				,"contractnum"=>@$_POST['contractnum'], "actualnum"=>@$_POST['actualnum'], "period"=>@$_POST['period']
				,"touserid"=>@$_POST['touserid'], "tofee"=>@$_POST['tofee'], "didtopay"=>@$_POST['didtopay']
				, "remark"=>@$_POST['remark'], "srctype"=>@$_POST['srctype'], "srcuserid"=>@$_POST['srcuserid']
				, "srcfee"=>@$_POST['srcfee'], "didpaysrc"=>@$_POST['didpaysrc'], "recuserid"=>@$_POST['recuserid']
				, "recfee"=>@$_POST['recfee'], "didpayrec"=>@$_POST['didpayrec'], "deleted"=>@$_POST['deleted']
				, "lastcomm"=>parsedate(@$_POST['lastcomm']), "nextcomm"=>parsedate(@$_POST['nextcomm'])
				, "commrecord"=>@$_POST['commrecord'], "evalcustomer"=>@$_POST['evalcustomer'], "toremark"=>@$_POST['toremark']
				, "srcremark"=>@$_POST['srcremark'], "recremark"=>@$_POST['recremark']
			)
			,"id=$_POST[id]");
		if($count>0){
			$retparams['errorcode'] = 0;
		}
		else{
			$retparams['errorcode'] = -1;
		}
		return BEND_ECHORET;
	}
	elseif("search"==$_POST['op']){
		return BEND_HOMEPAGE;
	}
	return BEND_REDIRECT;
}
?>